import { handlerGobackClick, hint } from "../../../utils/util";
import { Collect } from "../../../models/collect";
import { config } from "../../../config"
const collect = new Collect()
Page({
  data: {
    show: false,
    imgUrl: "",
    devicePosition: "front",
    tip: "",
    active: false,

    paiNew: wx.getStorageSync("paiNew"),
  },
  handlerGobackClick,
  onLoad: function (options) {},
  onReady: function () {},
  onShow: function () {},
  //切换镜头
  cutCamera() {
    let devicePosition = this.data.devicePosition;
    if (devicePosition == "front") {
      devicePosition = "back";
    } else {
      devicePosition = "front";
    }
    this.setData({
      devicePosition,
      show: false,
    });
  },
  //拍照
  takePhotos() {
    let { show } = this.data;
    if (show) {
      this.setData({
        show: false,
        tip: "",
        imgUrl: "",
      });
    } else {
      hint("正在拍摄...");
      const ctx = wx.createCameraContext();
      ctx.takePhoto({
        quality: "low",
        success: (res) => {
          let that = this;
          if (res.tempImagePath) {
            let img = wx
              .getFileSystemManager()
              .readFileSync(res.tempImagePath, "base64");
            let data = { base64: img };
            wx.request({
              url: config.faceUrl + "/Mobile/GetFace",
              method: "POST",
              data: data,
              success: (res) => {
                console.log(res.data);
                let result = res.data;
                if (result.code == 1000) {
                  if (result.eyes < 1) {
                    that.setData({
                      show: false,
                      tip: "请正对屏幕重试",
                      imgUrl: "",
                      active: true,
                    });
                    return;
                  }
                  if (result.faces > 1) {
                    hint("请勿出现多张人脸");
                    that.setData({
                      show: false,
                      imgUrl: "",
                      active: true,
                    });
                    return;
                  }
                  that.setData({
                    show: true,
                    tip: "人脸识别成功",
                    imgUrl: result.data,
                    active: false,
                  });
                } else {
                  that.setData({
                    show: false,
                    tip: "请正对屏幕重试",
                    imgUrl: "",
                    active: true,
                  });
                }
              },
              fail: (res) => {
                hint(res.errMsg);
              },
            });
          }
        },
      });
    }
  },
  //提交
  submit() {
    let { imgUrl } = this.data;
    if (!imgUrl) {
      hint("请重新拍摄照片");
      return;
    }
    let userinfo = wx.getStorageSync("userinfoData");
    let data = {
      imgBase64: imgUrl,
      name: userinfo.userName,
      phone: userinfo.userPhone,
      commid: userinfo.selectKey,
      idCard: userinfo.userIdCard,
      buildingCode: userinfo.building,
      unitCode: userinfo.unit,
      floorCode: userinfo.floor,
      roomCode: userinfo.room,
    };
    hint("正在提交...", "loading");
    collect.submit(data).then((res) => {
      if (res.code == 1000) {
        hint(res.message);
        this.setData({
          imgUrl: "",
        });
        wx.navigateBack();
      } else {
        hint(res.message);
      }
    });
  },
});